package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;

import ejb.ApplyFacadeEJBRemote;
import ejb.UserFacadeEJBRemote;

import model.Apply;
import model.Myuser;
import model.Target;


@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
	@EJB UserFacadeEJBRemote uf;
	@EJB ApplyFacadeEJBRemote af;
    @Resource private UserTransaction utx; 
    @PersistenceContext EntityManager em;
	
	
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		String userid = (String) request.getParameter("userid");
		String duserid = (String) request.getParameter("duserid");
		
		try{
			if(username==null){
	            out.print("Please login first! redirecting to Login...");
	            response.setHeader("Refresh","2;URL=login.jsp");
			}
			if(!(uf.findUserByName(username).getAuthority().equals("editor")||uf.findUserByName(username).getAuthority().equals("admin"))){
	            out.print("Oops, seems you do not have the authority to visit the editor page. Redirecting to Home page...");
	            response.setHeader("Refresh","2;URL=home.jsp");
			}
			
			//have authority
			/* Review merchant application */
			if (userid!=null){
				
				int uid = Integer.parseInt(userid);
				try{
					utx.begin();
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
				
				Myuser u = new Myuser();
				u = uf.findUserByID(uid);
								
				u.setAuthority("editor");
				em.merge(u);
								
				try{
					utx.commit();
				}
				catch(Exception ex)					
				{
					ex.printStackTrace();
				}
				
			}
			
			
			if (duserid!=null){
				
				int duid = Integer.parseInt(duserid);
				try{
					utx.begin();
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
				
				Myuser du = new Myuser();
				du = uf.findUserByID(duid);
				
				out.println(du.getUsername());
				
				em.remove(em.merge(du));
								
				try{
					utx.commit();
				}
				catch(Exception ex)					
				{
					ex.printStackTrace();
				}
				
			}
			
			out.println("here");

				
			/* forward all the application to jsp */
			List<Myuser> userlist = uf.getUserByAuthority("personal user");
			List<Myuser> userlist_e = uf.getUserByAuthority("editor");
			List<Myuser> userlist_m = uf.getUserByAuthority("merchant");
			List<Myuser> userlist_a = uf.getUserByAuthority("admin");
			
		    request.setAttribute("userlist", userlist);
		    request.setAttribute("userlist_e", userlist_e);
		    request.setAttribute("userlist_m", userlist_m);
		    request.setAttribute("userlist_a", userlist_a);

		    request.getRequestDispatcher("admin.jsp").forward(request, response);
				
		} catch (Exception e) {
			e.printStackTrace();				
	}
		
}

	

	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
